package com.byt.framlib.commonwidget.recycler;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

public class MyRecyclerViewScrollListener extends RecyclerView.OnScrollListener {

    private ImageView rebackTop;

    public MyRecyclerViewScrollListener(ImageView rebackTop) {
        this.rebackTop = rebackTop;
    }

    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);
        LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();
        int firstVisibleItemPosition = manager.findFirstVisibleItemPosition();
        // 当不滚动时
        if (newState == RecyclerView.SCROLL_STATE_IDLE) {
            // 判断是否滚动超过一屏
            if (firstVisibleItemPosition == 0) {
                rebackTop.setVisibility(View.GONE);
            } else {
                rebackTop.setVisibility(View.VISIBLE);
            }
        } else if (newState == RecyclerView.SCROLL_STATE_DRAGGING) {//拖动中
            rebackTop.setVisibility(View.GONE);
        }
    }
}